package com.zl.view;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;

import com.zl.dao.BookBorrowDao;
import com.zl.dao.MessageDao;
import com.zl.dao.UserDao;
import com.zl.model.User;
import com.zl.util.ZLString;
import com.zl.util.ZLUtil;

import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UserInfoFrame extends JInternalFrame {
	private JTextField textField;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserInfoFrame frame = new UserInfoFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public UserInfoFrame() {
		setTitle("人员管理");
		setClosable(true);
		setIconifiable(true);
		setBounds(100, 100, 549, 422);
		
		JScrollPane scrollPane = new JScrollPane();
		
		JLabel label = new JLabel("用户名：");
		
		textField = new JTextField();
		textField.setColumns(10);
		
		JButton button = new JButton("查询");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				userSearch(e);
			}
		});
		button.setIcon(new ImageIcon(UserInfoFrame.class.getResource("/images/search.png")));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(37)
							.addComponent(label)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(textField, GroupLayout.PREFERRED_SIZE, 335, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(button))
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 525, Short.MAX_VALUE)))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(32)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(button))
					.addGap(18)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
				new Object[][] {
				},
				new String[] {
					"\u7F16\u53F7", "\u7528\u6237\u540d", "\u6027\u522b", "\u501f\u9605\u56fe\u4e66\u6570\u91cf", "\u9884\u7ea6\u56fe\u4e66\u540d\u79f0"
				}
			) {
				boolean[] columnEditables = new boolean[] {
					false, false, false, false, false
				};
				public boolean isCellEditable(int row, int column) {
					return columnEditables[column];
				}
			});
		table.getColumnModel().getColumn(4).setPreferredWidth(150);
		scrollPane.setViewportView(table);
		getContentPane().setLayout(groupLayout);

		this.fillTable(new User());
	}
	
	/**
	 * 用户搜索
	 */
	private void userSearch(ActionEvent e) {
		// TODO Auto-generated method stub
		String userName = this.textField.getText();
		
		User user = new User();
		user.setUserName(userName);
		this.fillTable(user);
	}

	
	/**
	 * 初始化表格数据
	 * @param book
	 */
	private void fillTable(User user){
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.setRowCount(0); // 设置成0行 就是清空数据

		Connection connection = null;
		try{
			connection = ZLUtil.getConnect();
			ResultSet set = UserDao.select(connection, user);
			Vector v = null;
			while(set.next()){
				
				v=new Vector();
				int userId = set.getInt("id");
				v.add(userId);
				v.add(set.getString("userName"));
				v.add(set.getString("sex"));
				//借阅图书数
				int bookBorrowCount = 0;
				try {
//
					ResultSet set1 = BookBorrowDao.selectNotReturn(connection, userId);
					set1.last();
					bookBorrowCount = set1.getRow();
//					while (set.next()) {//循环获取记录数
//						bookBorrowCount++;
//					}
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
					JOptionPane.showMessageDialog(null, "查询用户借阅信息失败！");
				}
				v.add(bookBorrowCount);
				//预约图书名
				String appointmentBookName = "";
				try {
					
					ResultSet set2 = BookBorrowDao.selectNotAppointMent(connection, userId);
					while (set2.next()) {
						appointmentBookName = set2.getString("bookName");
					}
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
					JOptionPane.showMessageDialog(null, "查询用户预约信息失败！");
				}
				v.add(appointmentBookName);
				dtm.addRow(v);
			}
			
			
			
		}catch(Exception e){
			
			e.printStackTrace();
		}finally{
			try {
				ZLUtil.colseConnect(connection);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
